-- GuardStatue.lua
-- Created by huanghr July/13/2016
-- 守卫雕像

-- 初始化
function initGuardStatue(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;

    local dbase = DungeonM.query(class, "dbase");
    local sealItemCost  = dbase["seal_item"];

    if type(sealItemCost) ~= "table" then
        error("没有配置解除封印所需的物品。");
        return;
    end

    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 音效
            AudioM.playFx("workshop_open_ui");

            local pos = self.gridData:getPos();

            -- 解除封印
            local function okFunc()
                if ItemM.getAmount(ME.user, sealItemCost[2]) < sealItemCost[3] then
                    alert(getLocStr("cant_break_seal"));
                else
                    DungeonActionM.go("break_seal", pos);
                end
            end

            self:openBuildingView(okFunc);
        end
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
end
